﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.events.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;

    public class NeighborNavigationManager implements IGameWorldStateObserver {

        private const VISIT_NEIGHBOR:String = "visitNeighbor";
        private const CHANGE_NEIGHBOR:String = "changeNeighbor";

        private var m_world:GameWorld;
        private var m_neighborNavigators:Array;
        private var m_currentActiveNeighborNavigator:NeighborNavigator;
        private var m_friendsPictures:Array;
        private var m_friendsPicturesIDs:Array;
        private var m_maxNeighborNavigators:Number;

        public function NeighborNavigationManager(_arg1:GameWorld){
            this.m_neighborNavigators = [];
            this.m_friendsPictures = new Array();
            this.m_friendsPicturesIDs = new Array();
            super();
            this.m_world = _arg1;
            _arg1.addObserver(this);
        }
        public function initialize():void{
            this.m_neighborNavigators = [];
            this.m_maxNeighborNavigators = 0;
        }
        public function cleanUp():void{
            this.m_neighborNavigators = null;
            this.m_currentActiveNeighborNavigator = null;
            this.m_friendsPictures = null;
            this.m_friendsPicturesIDs = null;
        }
        public function get neighborNavigators():Array{
            return (this.m_neighborNavigators);
        }
        public function get friendsPictures():Array{
            return (this.m_friendsPictures);
        }
        public function get friendsPicturesIDs():Array{
            return (this.m_friendsPicturesIDs);
        }
        public function clearFriendsPicturesData():void{
            this.m_friendsPictures = new Array();
            this.m_friendsPicturesIDs = new Array();
        }
        public function addFriendPictureData(_arg1:DisplayObject, _arg2:String):void{
            this.m_friendsPictures.push(_arg1);
            this.m_friendsPicturesIDs.push(_arg2);
        }
        public function findSelectedFriendPictureIndex(_arg1:Player):Number{
            var _local2:Number = 0;
            while (_local2 < this.m_friendsPicturesIDs.length) {
                if (this.m_friendsPicturesIDs[_local2] == _arg1.uid){
                    return (_local2);
                };
                _local2++;
            };
            return (-1);
        }
        public function onGameLoaded(_arg1:Object):void{
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function loadTopFriendsIntoNavigators():void{
            var _local2:Number;
            var _local1:Number = 0;
            while (_local1 < Global.player.topFriends.length) {
                if (Global.player.topFriends[_local1] != "-1"){
                    _local2 = 0;
                    while (_local2 < Global.player.friends.length) {
                        if (Global.player.friends[_local2].uid == Global.player.topFriends[_local1]){
                            this.m_neighborNavigators[_local1].currentTopNeighbor = Global.player.friends[_local2];
                            break;
                        };
                        _local2++;
                    };
                    this.m_neighborNavigators[_local1].containsNeighbor = true;
                    this.m_neighborNavigators[_local1].setItem(Global.gameSettings.getItemByName("prop_filled_top_neighbor"));
                    this.m_neighborNavigators[_local1].reloadImage();
                    this.m_neighborNavigators[_local1].loadTopFriendPicture();
                };
                _local1++;
            };
        }
        public function onPlayerLoaded():void{
            var _local1:Number;
            if (Global.player.topFriends.length == 0){
                _local1 = 0;
                while (_local1 < 2) {
                    Global.player.topFriends.push("-1");
                    _local1++;
                };
                GameTransactionManager.addTransaction(new TUpdateTopFriends(Global.player.topFriends));
            };
            this.loadTopFriendsIntoNavigators();
        }
        private function createNeighborNavigator(_arg1:String):void{
            var neighborNavigator:* = null;
            var itemName:* = _arg1;
            neighborNavigator = new NeighborNavigator(itemName);
            neighborNavigator.setPlayActionCallback(function ():void{
                onNeighborNavigatorPlayAction(neighborNavigator);
            });
            neighborNavigator.setOuter(this.m_world);
            neighborNavigator.attach();
            neighborNavigator.containsNeighbor = false;
            this.m_neighborNavigators.push(neighborNavigator);
        }
        private function onNeighborNavigatorPlayAction(_arg1:NeighborNavigator):void{
            var _local2:Array;
            this.m_currentActiveNeighborNavigator = _arg1;
            if (_arg1.containsNeighbor){
                _local2 = [{
                    label:"Visit Neighbor",
                    action:this.VISIT_NEIGHBOR
                }, {
                    label:"Change Neighbor",
                    action:this.CHANGE_NEIGHBOR
                }];
                UI.displayMenu(_local2, this.onMenuClicked);
            } else {
                this.addTopNeighborsDialog(_arg1);
            };
        }
        private function onMenuClicked(_arg1:MouseEvent):void{
            var _local2:ContextMenuItem = ((_arg1.target is ContextMenuItem)) ? (_arg1.target as ContextMenuItem) : null;
            if (((_local2) && (_local2.action))){
                switch (_local2.action){
                    case this.VISIT_NEIGHBOR:
                        UI.visitNeighbor(this.m_currentActiveNeighborNavigator.currentTopNeighbor.uid);
                        break;
                    case this.CHANGE_NEIGHBOR:
                        this.addTopNeighborsDialog(this.m_currentActiveNeighborNavigator);
                        break;
                };
            };
        }
        private function addTopNeighborsDialog(_arg1:NeighborNavigator):void{
            var _local3:TopFriendSelector;
            var _local2:Number = Global.friendbar.length;
            if (_local2 > 0){
                _local3 = new TopFriendSelector(_arg1);
                UI.displayPopup(_local3);
            } else {
                UI.displayMessage("view chart...!!", GenericDialogView.TYPE_OK);
            };
        }

    }
}//package Classes.sim 
